﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StudentManage.Data;

namespace StudentManage.Web.Admin.Management.SysManage
{
    public partial class YXAndBJ : SecurityPage
    {
        public override string PrivCode()
        {
            return "5566b1979049f032";
        }

        private void TVBangding()
        {
            this.TV_Student.Nodes.Clear();
            List<xy_bmxbm> list = xy_bmxbm.All().ToList();
            List<xy_bj> bjlist = xy_bj.All().ToList();

            List<DataAssociate> associatelist = DataAssociate.Where("Typeid=@0", Convert.ToInt32(EnumData.AssociateType.系别与班级)).ToList();
            list.Where(p=>p.BMFQID=="03").ToList().ForEach(j =>
            {
                TreeNode xynode = new TreeNode();
                xynode.Text = j.BMMC;
                xynode.Value = j.BMDM;
                xynode.Target = Convert.ToInt32(EnumData.TvNodeType.学院).ToString();
                xynode.ShowCheckBox = true;

                list.Where(p => p.BMFQID == j.BMDM&&p.BMMC.Contains("系")).ToList().ForEach(p =>
                {
                    TreeNode node = new TreeNode();
                    node.Text = p.BMMC;
                    node.Value = p.BMDM;
                    node.Target = Convert.ToInt32(EnumData.TvNodeType.系).ToString();
                    node.ShowCheckBox = true;

                    var s = from B in associatelist where B.ParentNo == p.BMDM select B.ChildNo;
                    var q = from A in bjlist
                            where s.Contains(A.BJDM)
                            select A;
                    List<xy_bj> childlist = q.ToList();
                    childlist.ForEach(o =>
                    {
                        TreeNode childNode = new TreeNode();
                        childNode.Text = o.BJMC;
                        childNode.Value = o.BJDM;
                        childNode.Target = Convert.ToInt32(EnumData.TvNodeType.班级).ToString();
                        childNode.ShowCheckBox = true;
                        node.ChildNodes.Add(childNode);
                    });
                    xynode.ChildNodes.Add(node);
                });

                TV_Student.Nodes.Add(xynode);
            });
            this.TV_Student.CollapseAll();
        }

        private void RepBanding()
        {
            var q = from A in LinqUtility.LinqContext.xy_bj
                    where !(from B in LinqUtility.LinqContext.DataAssociate
                            where B.Typeid == Convert.ToInt32(EnumData.AssociateType.系别与班级)
                            select B.ChildNo).Contains(A.BJDM) 
                            &&A.BJMC.Contains(this.txt_BJMC.Text.Trim())
                            &&A.NJ.Contains(this.txt_NJ.Text.Trim())
                    select A;

            //添加所选学院条件筛选
            if (this.TV_Student.SelectedNode != null)
            {
                TreeNode snode = this.TV_Student.SelectedNode;
                if (snode.Target == Convert.ToInt32(EnumData.TvNodeType.系).ToString())
                {
                    snode = snode.Parent;
                }

                q= from A in q 
                   where A.XYDM==snode.Value
                   select A;
            }

            int RowCount = q.Count();
            this.AspNetPager1.RecordCount = RowCount;
            int PageSize = this.AspNetPager1.PageSize;
            int SkipCount = PageSize * (this.AspNetPager1.CurrentPageIndex - 1);

            List<xy_bj> list = q.Skip(SkipCount).Take(PageSize).ToList();
            this.Rep_DataList.DataSource = list;
            this.Rep_DataList.DataBind();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            RepBanding();
            TVBangding();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            this.RepBanding();
        }

        protected void TV_Student_SelectedNodeChanged(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.RepBanding();
            if (this.TV_Student.SelectedNode == null || this.TV_Student.SelectedNode.Target != Convert.ToInt32(EnumData.TvNodeType.系).ToString())
            {
                return;
            }
            this.Lab_xq.Text = this.TV_Student.SelectedNode.Text;
            this.HidSelectId.Value = this.TV_Student.SelectedNode.Value;
        }

        protected void Btn_SetAnd_Click(object sender, EventArgs e)
        {
            string ParentNo = this.HidSelectId.Value;
            string ChildNoAll = this.hidSelectItem.Value.TrimEnd(',');

            foreach (string childNo in ChildNoAll.Split(','))
            {
                DataAssociate md = new DataAssociate();
                md.ParentNo = ParentNo;
                md.ChildNo = childNo;
                md.Typeid = Convert.ToInt32(EnumData.AssociateType.系别与班级);
                DataAssociate.Insert(md);
            }

            RepBanding();
            TVBangding();
        }

        protected void Btn_ClearAnd_Click(object sender, EventArgs e)
        {
            foreach (TreeNode node in this.TV_Student.CheckedNodes)
            {
                if (node.Target == Convert.ToInt32(EnumData.TvNodeType.班级).ToString())
                {
                    DataAssociate.Delete("ChildNo=@0 and TypeId=@1", node.Value, Convert.ToInt32(EnumData.AssociateType.系别与班级));
                }
            }
            RepBanding();
            TVBangding();
        }

        protected void Btn_Search_Click(object sender, EventArgs e)
        {
            this.AspNetPager1.CurrentPageIndex = 1;
            this.RepBanding();
        }
    }
}